package Control;

import Database.DBCustomer;
import Model.Customer;

public class CustomerCtrl {
    Customer customerModel;

    DBCustomer cust = new DBCustomer();

    public String createCustomer(String name, String address, String zipCode, String city, long phoneNumber, String type, int customerID) {
	String message = null;
	if (name != null && address != null && zipCode != null && city != null && phoneNumber != 0 && type != null && customerID != 0) {

	    if (cust.findCustomer(customerID).getId() != customerID) {
		customerModel = new Customer(name, address, zipCode, city, phoneNumber, type, customerID);

		try {
		    cust.insertCustomer(customerModel);
		    message = "Customer: " + name + " was created succesfuly!";
		} catch (Exception e) {

		    e.printStackTrace();
		}

	    } else {
		// Sent a message to the user that the input was wrong
		message = "Customer with this ID[" + customerID + "] already exist!";
	    }
	} else {
	    message = "Please fill up all fields!";

	}
	return message;
    }

    public String findCustomer(int customerID) {
	Customer customer = null;
	String message = null;
	try {
	    customer = cust.findCustomer(customerID);
	    System.out.println(customer.getName());
	} catch (Exception e) {

	    System.out.println("can't find");
	}

	if (customer.getId() == 0) { // id 0 must not exist

	    message = "Can't find customer!";
	} else {

	    message = "Customer: " + customer.getName() + "    [ID - " + customer.getId() + "]    [Address - " + customer.getAddress() + "]    [Phone - " + customer.getPhone() + "] ";
	}
	// Sent a message to the user that the input was wrong or the customer
	// was not found
	return message;

    }

    public String updateCustomer(String name, String address, String zipCode, String city, long phoneNumber, String type, int customerID) {
	String message = null;
	if (cust.findCustomer(customerID).getId() == customerID) {
	    customerModel = new Customer(name, address, zipCode, city, phoneNumber, type, customerID);
	    cust.updateCustomer(customerModel);
	    message = name + " was updated succesfuly!";
	} else {

	    message = "Customer doesn't exist!";
	}
	// We should either prombt for what information to update
	// or if we should force an update in all fields of the customer

	return message;
    }

    public String deleteCustomer(int customerID) {

	String message = null;
	if (cust.findCustomer(customerID).getId() == customerID) {
	    String name = cust.findCustomer(customerID).getName();
	    cust.deleteCustomer(customerID);

	    message = name + " was deleted succesfuly!";
	} else {

	    message = "Customer doesn't exist!";
	}
	// Here we need a script that removes the information
	// from the database

	return message;
    }

}
